package com.example.xxljobdemo.demos.service;

import com.alibaba.fastjson.JSON;
import com.example.xxljobdemo.demos.pojo.SeckillProductVo;
import com.example.xxljobdemo.demos.pojo.SeckillRedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SeckillProductServiceImpl {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public List<SeckillProductVo> queryByTimeFromCache(Integer time){

        String key = SeckillRedisKey.SECKILL_PRODUCT_HASH + time;
        List<Object> values = redisTemplate.opsForHash().values(key);
        List<SeckillProductVo> vos = new ArrayList<>();
        for (Object obj : values) {
            vos.add(JSON.parseObject((String) obj,SeckillProductVo.class));
        }
        return vos;
    }

}
